
#include "hw5.h"

typedef struct foo
{
	const char *word;
	int count;
} foo_t;


int cmp(const void *a, const void *b)
{
	foo_t *fa, *fb;
	fa = (foo_t*)a;
	fb = (foo_t*)b;

	return strcmp(fa->word, fb->word);
}

void walker(const void *node, const VISIT which, const int depth)
{
	if(which == 1 || which == 3)
	{
		foo_t *f;
		f = *(foo_t **)node;
		printf("\nWord is:%s\nCount is: %d\n", f->word, f->count);
	}
/*	else if(which == "leaf")
	{
		foo_t *f;
		f = *(foo_t **)node;
		printf("\nWord is:%s\nCount is: %d\n", f->word, f->count);
	}
*/
}

int main(int argc,char **argv)
{
	//int i, *ptr;
	void *root = NULL;
	const void *ret;
	foo_t *val, *val2;

	val = calloc(1, sizeof(foo_t));
	val->word = strdup("Lion");
	val->count = 1;
	//printf("word: %s\n", val->word);
	ret = tsearch(val, &root, cmp);
	printf("\nAdded Word: %s\n", (*(foo_t **)ret)->word);

	val2 = calloc(1, sizeof(foo_t));
	val2->word = strdup("Tiger");
	val2->count = 3;
	//printf("word: %s\n", val->word);
	ret = tsearch(val2, &root, cmp);
	printf("\nAdded Word: %d\n", (*(foo_t **)ret)->count);

	printf("Walking with twalk\n");
	twalk(root, walker);

	return 0;
}
